package de.openlaboratory.cmis.core;

import java.math.BigInteger;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Set;

import org.apache.chemistry.opencmis.client.api.ChangeEvents;
import org.apache.chemistry.opencmis.client.api.CmisObject;
import org.apache.chemistry.opencmis.client.api.Document;
import org.apache.chemistry.opencmis.client.api.Folder;
import org.apache.chemistry.opencmis.client.api.ItemIterable;
import org.apache.chemistry.opencmis.client.api.ObjectFactory;
import org.apache.chemistry.opencmis.client.api.ObjectId;
import org.apache.chemistry.opencmis.client.api.ObjectType;
import org.apache.chemistry.opencmis.client.api.OperationContext;
import org.apache.chemistry.opencmis.client.api.Policy;
import org.apache.chemistry.opencmis.client.api.QueryResult;
import org.apache.chemistry.opencmis.client.api.QueryStatement;
import org.apache.chemistry.opencmis.client.api.Relationship;
import org.apache.chemistry.opencmis.client.api.Repository;
import org.apache.chemistry.opencmis.client.api.Session;
import org.apache.chemistry.opencmis.client.api.Tree;
import org.apache.chemistry.opencmis.commons.SessionParameter;
import org.apache.chemistry.opencmis.commons.data.Ace;
import org.apache.chemistry.opencmis.commons.data.Acl;
import org.apache.chemistry.opencmis.commons.data.BulkUpdateObjectIdAndChangeToken;
import org.apache.chemistry.opencmis.commons.data.ContentStream;
import org.apache.chemistry.opencmis.commons.data.RepositoryInfo;
import org.apache.chemistry.opencmis.commons.definitions.TypeDefinition;
import org.apache.chemistry.opencmis.commons.enums.AclPropagation;
import org.apache.chemistry.opencmis.commons.enums.IncludeRelationships;
import org.apache.chemistry.opencmis.commons.enums.RelationshipDirection;
import org.apache.chemistry.opencmis.commons.enums.VersioningState;
import org.apache.chemistry.opencmis.commons.spi.CmisBinding;

import de.openlaboratory.cmis.repository.LocalFolder;
import de.openlaboratory.elib.core.LogManager;

public class OpenLabSession implements Session
{
	private Repository repository;
	private Map<String, String> parameters;
	private ObjectFactory objectFactory;;

	/**
	 * 
	 */
	private static final long serialVersionUID = 2503300113489676192L;

	public OpenLabSession(Repository repository)
	{
		this.repository = repository;
	}
	

	private ObjectFactory createObjectFactory()
	{
		Object objectFactory;
		
		try
		{
			String clz = parameters.get(SessionParameter.OBJECT_FACTORY_CLASS);

			if (clz == null)
			{
				objectFactory = new OpenLabObjectFactory();
			}
			else
			{
				objectFactory = Class.forName(clz).newInstance();
				
				if (!(objectFactory instanceof ObjectFactory))
				{
					throw new Exception("Wrong class name Setting for objectFactory in Session: " + clz);
				}
			}

			((ObjectFactory) objectFactory).initialize(this, parameters);

			return (ObjectFactory) objectFactory;
		}
		catch (Exception e)
		{
			throw new IllegalArgumentException("Object factory could not be created: " + e.getMessage(), e);
		}
	}

	/**
	 * CMIS INTERFACE IMPLEMENTATION
	 */

	@Override
	public Acl applyAcl(ObjectId arg0, List<Ace> arg1, List<Ace> arg2, AclPropagation arg3)
	{
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void applyPolicy(ObjectId arg0, ObjectId... arg1)
	{
		// TODO Auto-generated method stub

	}

	@Override
	public List<BulkUpdateObjectIdAndChangeToken> bulkUpdateProperties(List<CmisObject> arg0, Map<String, ?> arg1, List<String> arg2, List<String> arg3)
	{
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void clear()
	{
		// TODO Auto-generated method stub

	}

	@Override
	public ObjectId createDocument(Map<String, ?> arg0, ObjectId arg1, ContentStream arg2, VersioningState arg3)
	{
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public ObjectId createDocument(Map<String, ?> arg0, ObjectId arg1, ContentStream arg2, VersioningState arg3, List<Policy> arg4, List<Ace> arg5, List<Ace> arg6)
	{
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public ObjectId createDocumentFromSource(ObjectId arg0, Map<String, ?> arg1, ObjectId arg2, VersioningState arg3)
	{
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public ObjectId createDocumentFromSource(ObjectId arg0, Map<String, ?> arg1, ObjectId arg2, VersioningState arg3, List<Policy> arg4, List<Ace> arg5, List<Ace> arg6)
	{
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public ObjectId createFolder(Map<String, ?> arg0, ObjectId arg1)
	{
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public ObjectId createFolder(Map<String, ?> arg0, ObjectId arg1, List<Policy> arg2, List<Ace> arg3, List<Ace> arg4)
	{
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public ObjectId createItem(Map<String, ?> arg0, ObjectId arg1)
	{
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public ObjectId createItem(Map<String, ?> arg0, ObjectId arg1, List<Policy> arg2, List<Ace> arg3, List<Ace> arg4)
	{
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public ObjectId createObjectId(String arg0)
	{
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public OperationContext createOperationContext()
	{
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public OperationContext createOperationContext(Set<String> arg0, boolean arg1, boolean arg2, boolean arg3, IncludeRelationships arg4, Set<String> arg5, boolean arg6,
			String arg7, boolean arg8, int arg9)
	{
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public ObjectId createPolicy(Map<String, ?> arg0, ObjectId arg1)
	{
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public ObjectId createPolicy(Map<String, ?> arg0, ObjectId arg1, List<Policy> arg2, List<Ace> arg3, List<Ace> arg4)
	{
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public QueryStatement createQueryStatement(String arg0)
	{
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public ObjectId createRelationship(Map<String, ?> arg0)
	{
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public ObjectId createRelationship(Map<String, ?> arg0, List<Policy> arg1, List<Ace> arg2, List<Ace> arg3)
	{
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public ObjectType createType(TypeDefinition arg0)
	{
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void delete(ObjectId arg0)
	{
		// TODO Auto-generated method stub

	}

	@Override
	public void delete(ObjectId arg0, boolean arg1)
	{
		// TODO Auto-generated method stub

	}

	@Override
	public void deleteType(String arg0)
	{
		// TODO Auto-generated method stub

	}

	@Override
	public Acl getAcl(ObjectId arg0, boolean arg1)
	{
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public CmisBinding getBinding()
	{
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public ItemIterable<Document> getCheckedOutDocs()
	{
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public ItemIterable<Document> getCheckedOutDocs(OperationContext arg0)
	{
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public ChangeEvents getContentChanges(String arg0, boolean arg1, long arg2)
	{
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public ChangeEvents getContentChanges(String arg0, boolean arg1, long arg2, OperationContext arg3)
	{
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public ContentStream getContentStream(ObjectId arg0)
	{
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public ContentStream getContentStream(ObjectId arg0, String arg1, BigInteger arg2, BigInteger arg3)
	{
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public OperationContext getDefaultContext()
	{
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Locale getLocale()
	{
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public CmisObject getObject(ObjectId arg0)
	{
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public CmisObject getObject(String arg0)
	{
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public CmisObject getObject(ObjectId arg0, OperationContext arg1)
	{
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public CmisObject getObject(String arg0, OperationContext arg1)
	{
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public CmisObject getObjectByPath(String arg0)
	{
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public CmisObject getObjectByPath(String arg0, OperationContext arg1)
	{
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public ObjectFactory getObjectFactory()
	{
		if (this.objectFactory == null)
		{
			this.objectFactory = createObjectFactory();
		}
		
		return this.objectFactory;
	}

	@Override
	public ItemIterable<Relationship> getRelationships(ObjectId arg0, boolean arg1, RelationshipDirection arg2, ObjectType arg3, OperationContext arg4)
	{
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public RepositoryInfo getRepositoryInfo()
	{
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Folder getRootFolder()
	{
		if (this.repository != null)
		{
			String rootFolderId = this.repository.getRootFolderId();
			Folder rootFolder = new LocalFolder(rootFolderId);
			return rootFolder;
		} else
		{
			LogManager.logMessage("Session: Repository not set.");
		}
		return null;
	}

	@Override
	public Folder getRootFolder(OperationContext arg0)
	{
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public ItemIterable<ObjectType> getTypeChildren(String arg0, boolean arg1)
	{
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public ObjectType getTypeDefinition(String arg0)
	{
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<Tree<ObjectType>> getTypeDescendants(String arg0, int arg1, boolean arg2)
	{
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public ItemIterable<QueryResult> query(String arg0, boolean arg1)
	{
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public ItemIterable<QueryResult> query(String arg0, boolean arg1, OperationContext arg2)
	{
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public ItemIterable<CmisObject> queryObjects(String arg0, String arg1, boolean arg2, OperationContext arg3)
	{
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void removeObjectFromCache(ObjectId arg0)
	{
		// TODO Auto-generated method stub

	}

	@Override
	public void removeObjectFromCache(String arg0)
	{
		// TODO Auto-generated method stub

	}

	@Override
	public void removePolicy(ObjectId arg0, ObjectId... arg1)
	{
		// TODO Auto-generated method stub

	}

	@Override
	public Acl setAcl(ObjectId arg0, List<Ace> arg1)
	{
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void setDefaultContext(OperationContext arg0)
	{
		// TODO Auto-generated method stub

	}

	@Override
	public ObjectType updateType(TypeDefinition arg0)
	{
		// TODO Auto-generated method stub
		return null;
	}

}
